home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / KEYBOARD.SWG / 0076_Stuffing Keyboard Buffer.pas < prev    next >
Pascal/Delphi Source File  |  1994-02-05  |  3KB  |  128 lines

  1. {
  2.    I found the following code in my Utils unit and repackaged into a new
  3.    unit. It compiles and works on my PC, but as Im sure everyone will
  4.    agree, this programming style is rather dirty! It assumes the keyboard
  5.    buffer is in the same location in memory on every pc. I really should
  6.    be using an interrupt but never got around to it.
  7.    Remember that you have a limit of only 16 characters in the buffer.
  8.  
  9.    Anywayz hope it helps...
  10.  
  11.    {-----------------------------------CUT HERE------------------------}
  12.  
  13. UNIT KeyStuff;
  14.  
  15. Interface
  16.  
  17. Var
  18.   keyrec : array [1..16] of
  19.              record
  20.                ch ,
  21.                scan : char;
  22.              end absolute $0000:1054;
  23.   KeyPtr1: byte absolute 0000:1050;
  24.   KeyPtr2: byte absolute 0000:1052;
  25.  
  26.   Procedure AdvanceKeyTail;
  27.   Procedure AdvanceKeyHead;
  28.   procedure FlushBuffer;
  29.   procedure StuffBufferKey(ch,Scan : char);
  30.   procedure StuffBuffer(W:word);
  31.   procedure StuffBufferStr(Str:string);
  32.   function  tail:byte;
  33.   Function  head:byte;
  34.  
  35. Implementation
  36. uses dos;
  37.  
  38. Procedure AdvanceKeyTail;
  39. {Moves the keyboard tail ptr forward}
  40. begin
  41.   inc(KeyPtr1,2);
  42.   if keyptr1 > 60 then keyptr1 := 30;
  43. end;
  44.  
  45. Procedure AdvanceKeyHead;
  46. {Moves the keyboard Head ptr forward.
  47. Turbo's KeyPressed function will now return True}
  48. begin
  49.   inc(KeyPtr2,2);
  50.   if keyptr2 > 60 then keyptr2 := 30;
  51. end;
  52.  
  53. procedure FlushBuffer;
  54. {Clear Keyboard Buffer}
  55. var Regs: registers;
  56. begin
  57.    with Regs do
  58.    begin
  59.       Ax := ($0c shl 8) or 6;
  60.       Dx := $00ff;
  61.    end;
  62.    Intr($21,Regs);
  63. end;
  64.  
  65. procedure StuffBufferKey(ch,Scan : char);
  66. {Puts keyboard scan code directly into the buffer
  67. Examples. #65,#0 = Simulate an 'A' being pressed
  68.           #0,#59 = Simulate the F1 key being pressed
  69. }
  70. begin
  71.    keyrec[head].ch := ch;
  72.    keyrec[head].scan := scan;
  73.    AdvanceKeyhead;
  74. end;
  75.  
  76. procedure StuffBuffer(W:word);
  77. {Put Word directly into the buffer}
  78. begin
  79.    keyrec[head].ch := chr(lo(w));
  80.    keyrec[head].scan := chr(hi(w));
  81.    AdvanceKeyhead;
  82. end;
  83.  
  84. procedure StuffBufferStr(Str:string);
  85. {Stuffs a string into the buffer. Remember the max of 16 chars.}
  86. var I,L : byte;
  87. begin
  88.    if Str <> '' then
  89.    begin
  90.       I := 1;
  91.       L := length(Str);
  92.       while I <= L do
  93.       begin
  94.          StuffBuffer(ord(Str[I]));
  95.          inc(I);
  96.       end;
  97.    end;
  98. end;
  99.  
  100. function Tail:byte;
  101. {Returns number between 1 and 16 showing where tail is}
  102. begin
  103.   tail := KeyPtr1 div 2 - 14;
  104. end;
  105.  
  106. Function Head:byte;
  107. {Returns number between 1 and 16 showing where head is}
  108. begin
  109.   head := KeyPtr2 div 2 - 14;
  110. end;
  111.  
  112. end.
  113.  
  114.    {-----------------------------------CUT HERE------------------------}
  115.  
  116. *********************
  117. Example of use...
  118. *********************
  119.  
  120. Uses Keystuff;
  121.  
  122. begin
  123.   StuffBufferStr('Hello There!'#13);
  124.   Write('>');
  125.   Readln;
  126.   halt;
  127. end.
  128.